<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\Validate;

class Train extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    public function list(){
        $type = $this->request->post('type');
        if(!$type) $this->error("缺少必传参数");
        $re_data =  Db::name("train_article")->where(['type'=>$type,'delete_time'=>null])->select();
        $config = get_addon_config('alioss');
        foreach ($re_data as $k=>$v){
            $re_data[$k]['img_main'] =  $config['cdnurl'].$v['img_main'];
        }

        $this->success('成功',$re_data);
    }
    public function detail(){
        $id = $this->request->post('id');
        if(!$id) $this->error("缺少必传参数");
        $re_data =  Db::name("train_article")->where(['id'=>$id])->find();
        $config = get_addon_config('alioss');
        $re_data['img_main'] = $config['cdnurl'].$re_data['img_main'];
        $this->success('成功',$re_data);
    }
    /**
     * @return void
     * 报名申请
     */
    public function trainEnrollAdd(){
        $user_id=$this->auth->id;
        $name = $this->request->post('name');
        $article_id = $this->request->post('article_id');
        $age = $this->request->post('age');
        $duties = $this->request->post('duties');
        $tel = $this->request->post('tel');
        $mail = $this->request->post('mail');
        $address = $this->request->post('address');
        $company = $this->request->post('company');
        if(!$name || !$age || !$duties || !$tel || !$mail || !$address || !$company || !$article_id) $this->error("缺少必传参数");
        if ($mail && !Validate::is($mail, "email")) {
            $this->error(__('Email is incorrect'));
        }
        if ($tel && !Validate::regex($tel, "^1\d{10}$")) {
            $this->error(__('Mobile is incorrect'));
        }
        $add_data['uid'] = $user_id;
        $add_data['article_id'] = $article_id;
        $add_data['name'] = $name;
        $add_data['age'] = $age;
        $add_data['duties'] = $duties;
        $add_data['tel'] = $tel;
        $add_data['mail'] = $mail;
        $add_data['address'] = $address;
        $add_data['company'] = $company;
        $add_data['createtime'] = time();
        $add_data['updatetime'] = time();
        $add1 = Db::name("train_enroll")->insert($add_data);
        if($add1){
            $this->success('成功');
        }else{
            $this->error("失败");
        }
    }

    /**
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 讲师列表
     */
    public function teacherList(){
        $re_data =  Db::name("train_teacher")->select();
        $this->success('成功',$re_data);
    }
}
